package util

import (
	"io"
	"net/http"
	"strings"
)

func Get(url, data string, contentTypes []string) (status, responseText string, err error) {
	return Call(url, data, "GET", contentTypes)
}

func Post(url, data string, contentTypes []string) (status, responseText string, err error) {
	return Call(url, data, "POST", contentTypes)
}

func Update(url, data string, contentTypes []string) (status, responseText string, err error) {
	return Call(url, data, "UPDATE", contentTypes)
}

func Delete(url, data string, contentTypes []string) (status, responseText string, err error) {
	return Call(url, data, "DELETE", contentTypes)
}

func Call(url, data, method string, contentTypes []string) (status, responseText string, err error) {
	client := &http.Client{}
	req, err := http.NewRequest(method, url, strings.NewReader(data))
	if err != nil {
		return
	}

	for _, contentType := range contentTypes {
		req.Header.Set("Content-Type", contentType)
	}

	resp, err := client.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return
	}

	return resp.Status, string(body), err

}
